from time import sleep

from selenium.webdriver.common.by import By

from demo.weworkSelenium.page.base_page import BasePage


class Message(BasePage):
    def send_message(self, app='', group='', content=''):
        js_select_apps_btn = (By.CSS_SELECTOR, '.js_select_apps_btn')

        js_app_dlg_item = (By.CSS_SELECTOR, '.js_app_dlg_item[data-name="%s"]' % app)
        queding = (By.LINK_TEXT, '确定')
        js_select_range_btn = (By.CSS_SELECTOR, '.js_select_range_btn')


        memberSearchInput = (By.CSS_SELECTOR, '#memberSearchInput')

        ww_searchResult_itemTxt = (By.CSS_SELECTOR, '.ww_searchResult_itemTxt')
        queren = (By.LINK_TEXT, '确认')
        js_send_msg_text = (By.CSS_SELECTOR, '.js_send_msg_text')
        send = (By.LINK_TEXT, '发送')
        queding_last = (By.LINK_TEXT, '确定')

        self.find(js_select_apps_btn).click()
        self.find(js_app_dlg_item).click()
        self.find(queding).click()

        #关闭弹窗之后，蒙层还未消失，影响点击事件，需要等到蒙层消失
        self.wait_hide(By.CSS_SELECTOR, '.js_dialog_mask')
        self.find(js_select_range_btn).click()

        # Todo memberSearchInput是动态生成的元素，多次发送消息，之前的元素并没有消失,每次都需要取最新都元素
        # self.find(memberSearchInput).send_keys('%s' % group)
        self._driver.find_elements(*memberSearchInput)[-1].send_keys('%s' % group)

        self.find(ww_searchResult_itemTxt).click()
        self.find(queren).click()
        self.wait_hide(By.CSS_SELECTOR, '.js_dialog_mask')
        self.find(js_send_msg_text).send_keys('%s' % content)
        self.find(send).click()
        self.find(queding_last).click()





